<?php
session_start();

define('MEKARSARI_WEBGIS', 2009);
define('_FILENAME_', 'map.php');
define('_PATH_', str_replace(_FILENAME_, '',  str_replace('\\', '/', __FILE__)));
 
//------------------------------------------
// Arrow - display navigation arrows for IE
include "config.php";
include "include/functions.php";
updateDBF($db, 'data/kavling_kurang_.dbf');

// getting information about planted-non planted 
$info = getDBFdata('data/kavling_kurang_.dbf');


$sid = trim($_GET['sid']);
// IE .. session transfer.
$browser = getBrowser();
if( !(($browser['agent'] == 'FIREFOX' && $browser['version'] >= '2.0') ||  
	    ($browser['agent'] == 'OPERA' && $browser['version'] >= '9.0')) ){
		// copy session
		$q = 'SELECT `session_data` FROM caramel_managerial.session_data WHERE `session_id`="'.$sid.'"';
		$session_data = $db->get_var($q);
		if( strlen($session_data) < 1 ) die ('Database ERROR, contact your administrator');
		
		$session_data = unserialize($session_data);
		
		// register to current session
		foreach($session_data as $key=>$val){
			$_SESSION[$key] = $val;
		}
}

// path defaults
$map_path = _PATH_."data/";
$map_file = "mekarsari_kurang.map";
$img_path = _PATH_."temp/";

// Navigation defaults
$zoomsize 	= 2;
$zoomout 	= "";
$zoomin 	= "";

// Map mode
$browse = "CHECKED";
$nquery = "";
$radius = 5;          // sets TOLERANCE for point query

// reference map size
$ref_h = 148;
$ref_w = 200;

// default click x, y, and pan
// Default click point

define("X_CENTER", 494);
define("Y_CENTER", 308);

$clickx = X_CENTER;
$clicky = Y_CENTER;

$clkpoint 	= ms_newPointObj();
$old_extent = ms_newRectObj();
 
// Default extent & maximum extent are the same
$extent 	= array(167, 191, 952, 685);   
$maxextent 	= array(167, 191, 952, 685);   

$max_extent = ms_newRectObj();
$max_extent->setextent(167, 191, 952, 685);  

// Retrieve mapfile and create a map from it
$map = ms_newMapObj($map_path.$map_file);

// First time we're invoked use default variable and drop
// through to create map image for the first time else use
// form variables to pan and zoom before creating map image
if (
	( $_POST['img_x'] and $_POST['img_y'] ) or 
    $_POST['zoomIn'] or 
    $_POST['zoomOut'] or 
	$_POST['left_x'] or 
    $_POST['right_x'] or 
	$_POST['up_x'] or 
	$_POST['down_x'] or
	$_POST['ref_x'] or
	$_POST['ref_y']	or
	$_POST['labelZona'] or
	$_POST['labelBatas'] or
	$_POST['refresh']
	){
	 
	 // since we were invoked by the form - retrieve previous map extent     
	 if ( $_POST['extent'] ) {
		  $extent = split(" ", $_POST['extent']);
	 }
	 
     // left arrow clicked - pan left 1 image width
     if ( $_POST['left_x'] ) {
          $clickx = 0;
          $clicky = Y_CENTER;

     // right arrow clicked - pan right 1 image width
     } elseif ( $_POST['right_x'] ) {
          $clickx = 2 * X_CENTER;
          $clicky = Y_CENTER;

     // up arrow clicked - pan up 1 image height
     } elseif ( $_POST['up_x'] ) {
          $clickx = X_CENTER;
          $clicky = 0;

     // down arrow clicked - pan down 1 image height
     } elseif ( $_POST['down_x'] ) {
          $clickx = X_CENTER;
          $clicky = 2*Y_CENTER;

	 } elseif ($_POST['ref_x']) {  
		  $map_h = $maxextent[3]-$maxextent[1];
		  $map_w = $maxextent[2]-$maxextent[0];
		  		  
		  $ref_x = $_POST['ref_x'];
		  $ref_y = $_POST['ref_y'];		  
			  
		  // Set the mouse click location (we need it to zoom) 
		  list($clickx, $clicky) = ref2img($ref_x, $ref_y, $ref_w, $ref_h, $map_w, $map_h);
		  // Set the mouse click location (we need it to zoom) 
		 
		  $clkpoint->setXY($clickx,$clicky);
		  list($qx,$qy) = img2map($map->width,$map->height,$clkpoint,$max_extent);
     } elseif ( 
			$_POST['refresh'] or 
			$_POST['zoomIn'] or 
			$_POST['zoomOut'] or 
			$_POST['labelZona'] or 
			$_POST['labelBatas']) {
	          $clickx = X_CENTER;
	          $clicky = Y_CENTER;
	 } else {
          $clickx = $_POST['img_x'];
          $clicky = $_POST['img_y'];
     }
	 	 
     // Set the mouse click location (we need it to zoom)    
     $clkpoint->setXY($clickx,$clicky);
	 
	 // Calculate the zoom factor to pass to zoomPoint method
     // and setup the pan and zoom variables for web page     
     //   zoomfactor = +/- N
     //   if N > 0 zooms in - N < 0 zoom out - N = 0 pan
	 $zoom_factor = isset($_POST['zoomIn']) ? $zoomsize : (isset($_POST['zoomOut']) ? (-1 * $zoomsize) : 1);	 	 
	 //  
	 if($_POST['labelZona'] == 1) {
		$label_zona = "CHECKED=CHECKED";
		$zona_layer = $map->getLayerByName('zona');
		$zona_layer->set('status', MS_ON);
	 } else { 
		$label_zona = "";
		$zona_layer = $map->getLayerByName('zona');
		$zona_layer->set('status', MS_OFF);
	 }	 
	 
	 if($_POST['labelBatas'] == 1) {
		$label_batas = "CHECKED=CHECKED";
		$batas_layer = $map->getLayerByName('batas');
		$batas_layer->set('status', MS_ON);
	 } else { 
		$label_batas = "";
		$batas_layer = $map->getLayerByName('batas');
		$batas_layer->set('status', MS_OFF);
	 }	 
	 
	 // bikin extent buatan.. 
	 if( $_POST['ref_x'] ){
		 $extent = fakeExtent($extent, $qx, $qy, $maxextent);
	 }		  
	 
	 // Set the map to the extent retrieved from the form     
	 $map->setExtent($extent[0],$extent[1],$extent[2],$extent[3]);

	 // Save this extent as a rectObj, we need it to zoom.
	 $old_extent->setextent($extent[0],$extent[1],$extent[2],$extent[3]);	

	if ($zoom_factor == 0)
		$zoom_factor = 1;
    // Zoom in (or out) to clkpoint
	$map->zoomPoint($zoom_factor,$clkpoint,$map->width,$map->height,$old_extent,$max_extent);
}



// Retrieve store table, 
// add points to the points-of-interest layer, 
// build tooltip imagemap

$qresult = $db->get_results('SELECT * FROM `caramel_managerial`.`point`', ARRAY_N);
AddPointsToMap( $map, $qresult );
$image_map = CreateTTimagemap($qresult,$map); 
 
// create unique names for map and reference images
$map_id 	= sprintf("%0.6d",rand(0,999999));
$image_name = "mekarsari".$map_id.".png";
$image_url	="temp/".$image_name;
$ref_name 	= "reference".$map_id.".gif";
$ref_url	="temp/".$ref_name;
$leg_name 	= "thirdleg".$map_id.".png";
$leg_url	="temp/".$leg_name;
// Draw and save map image
$image=$map->draw();
$image->saveImage($img_path.$image_name);

// Draw and save reference image
$ref = $map->drawReferenceMap();
$ref->saveImage($img_path.$ref_name);

// Draw legend
$leg = $map->drawLegend();
$leg->saveImage($img_path.$leg_name);

// Get new extent of map (we'll save it in a form variable)
$new_extent = sprintf("%3.6f",$map->extent->minx)." "
             .sprintf("%3.6f",$map->extent->miny)." "
             .sprintf("%3.6f",$map->extent->maxx)." "
             .sprintf("%3.6f",$map->extent->maxy);

// get the scale of the image to display on the web page
$scale = sprintf("%10d",$map->scale);

// Convert mouse click from image coordinates to map coordinates
list($mx,$my) = img2map($map->width,$map->height,$clkpoint,$old_extent);
$mx_str = sprintf("%3.6f",$mx);
$my_str = sprintf("%3.6f",$my);
?>